package com.ysu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysu.demo.dataobject.GoodPeople;
import com.ysu.demo.enums.ExceptionEnum;
import com.ysu.demo.exception.BaseException;
import com.ysu.demo.from.goodpeople.GoodPeopleFrom;
import com.ysu.demo.from.goodpeople.GoodPeoplePages;
import com.ysu.demo.mapper.GoodPeopleMapper;
import com.ysu.demo.service.IGoodPeopleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lhw
 * @since 2019-08-21
 */
@Service
public class GoodPeopleServiceImpl extends ServiceImpl<GoodPeopleMapper, GoodPeople> implements IGoodPeopleService {

    @Override
    public Boolean c(GoodPeopleFrom goodPeopleFrom) {
        GoodPeople goodPeople = new GoodPeople();
        BeanUtils.copyProperties(goodPeopleFrom,goodPeople);

        return this.save(goodPeople);
    }

//    @Override
//    public IPage<GoodPeople> r(GoodPeoplePages pages) {
//        Integer page = (pages.getPages() == null)?1:pages.getPages();
//
//        QueryWrapper<GoodPeople> queryWrapper = new QueryWrapper<>();
//
//        queryWrapper.isNull("delete_time");
//
//        return this.page(new Page<GoodPeople>().setSize(20).setPages(page),queryWrapper);
//    }

    @Override
    public List<GoodPeople> r() {
        QueryWrapper<GoodPeople> queryWrapper = new QueryWrapper<>();

        queryWrapper.isNull("delete_time");

        return this.list(queryWrapper);
    }

    @Override
    public Boolean u(GoodPeopleFrom goodPeopleFrom) {
        GoodPeople goodPeople = this.getById(goodPeopleFrom.getId());

        if(goodPeople == null){
            throw new BaseException(ExceptionEnum.NOT_FIND);
        }

        BeanUtils.copyProperties(goodPeopleFrom,goodPeople);

        return this.updateById(goodPeople);
    }

    @Override
    public Boolean d(Integer id) {
        GoodPeople goodPeople = this.getById(id);

        if(goodPeople == null){
            throw new BaseException(ExceptionEnum.NOT_FIND);
        }

        goodPeople.setDeleteTime(new Date());

        return this.updateById(goodPeople);
    }
}
